let mongoose = require('mongoose'),
    async = require('async'),
    memberUsers = mongoose.model('member_users');

module.exports = (req, res) => {
    let params = req.body,
        msg = '', state = false;

    // 校验
    switch(true){
        case !params.mobile:
            msg = '手机号码不能为空！';
            state = true;
            break;
        case !params.userName:
            msg = '用户名不能为空！';
            state = true;
            break;
        case !params.passWord:
            msg = '密码不能为空！';
            state = true;
            break;
        default: break;
    }
    if(state){
        res.json(resFail(msg));
        return false;
    }

    async.series([
        // 验证用户
        callball => {
            memberUsers.findOne({
                userName: params.userName,
                mobile: params.mobile,
                passWord: params.passWord
            }, (err, doc) => {
                if(err){
                    res.json(resFail(err))
                }else{
                    if(doc){
                        this.userInfo = doc;
                        callball();
                    }else{
                        res.json(resFail('密码错误！'))
                    }
                }
            })
        },
        // 修改资料
        callball => {
            let condition = {};
            if(params.sex){
                condition.sex = params.sex;
            }
            if(params.speciality){
                condition.speciality = params.speciality;
            }
            if(params.comeFrom){
                condition.comeFrom = params.comeFrom;
            }
            if(params.address){
                condition.address = params.address;
            }
            if(params.birthday){
                condition.birthday = params.birthday;
            }
            if(params.newPassWord){
                condition.passWord = params.newPassWord;
            }
            if(params.avater){
                condition.avater = params.avater;
            }

            memberUsers.update({
                userName: params.userName,
                mobile: params.mobile
            }, {
                $set: condition
            }).exec(err => {
                if(err){
                    res.json(resFail(err))
                }else{
                    res.json({
                        result: 1,
                        data: {
                            success: 'success'
                        }
                    })
                }
            })
        }
    ])
}